﻿/*
 Copyright aswing.org, see the LICENCE.txt.
*/

import org.aswing.awml.ParserFactory;

/**
 * Provides utility routines for AWML parser.
 * 
 * @author Igor Sadovskiy
 */
class org.aswing.awml.AwmlUtils {
	
	/**
	 * Extracts pure node name from the passed XML element omitting namespace prefix.
	 * 
	 * @param xml the <code>XMLNode</code> to extract node name from
	 * @return extracted node name
	 */
	public static function getNodeName(xml:XMLNode):String {
		var idx:Number = xml.nodeName.indexOf(":");
		return (idx == -1) ? xml.nodeName : xml.nodeName.substr(idx + 1);
	}
	
	/**
	 * Searches the xml node for the child node with the specified name.
	 * 
	 * @param xml the parent XML node to search its children
	 * @param childName the child node namd to search
	 * @return found child node or <code>null</code>
	 */
	public static function getNodeChild(xml:XMLNode, childName:String):XMLNode {
		for (var i:Number = 0; i < xml.childNodes.length; i++) {
			if (getNodeName(xml.childNodes[i]) == childName) {
				return xml.childNodes[i];
			}	
		}
		return null;
	}
	
	/**
	 * Converts string represenration of the XML document to the XMLNode object.
	 * 
	 * @param str the string representation of the XML document to convert.
	 * @return XMLNode representation of the XML document.
	 */
	public static function strToXml(str:String):XMLNode {
        var xml:XML = new XML();
        xml.ignoreWhite = true;
        xml.parseXML(str);
        return xml.firstChild; 
	}
	
	/**
	 * Checks if passed <code>nodeName</code> belongs to component.
	 * 
	 * @param nodeName the node name to check
	 * @return <code>true</code> if belongs and <code>false</code> if not
	 */
	public static function isComponentNode(nodeName:String):Boolean {
		return ParserFactory.getInstance().isTypeOf(nodeName, ParserFactory.COMPONENT);
	}
	
	/**
	 * Checks if passed <code>nodeName</code> belongs to border.
	 * 
	 * @param nodeName the node name to check
	 * @return <code>true</code> if belongs and <code>false</code> if not
	 */
	public static function isBorderNode(nodeName:String):Boolean {
		return ParserFactory.getInstance().isTypeOf(nodeName, ParserFactory.BORDER);
	} 

	/**
	 * Checks if passed <code>nodeName</code> belongs to layout.
	 * 
	 * @param nodeName the node name to check
	 * @return <code>true</code> if belongs and <code>false</code> if not
	 */
	public static function isLayoutNode(nodeName:String):Boolean {
		return ParserFactory.getInstance().isTypeOf(nodeName, ParserFactory.LAYOUT);
	} 

	/**
	 * Checks if passed <code>nodeName</code> belongs to icon.
	 * 
	 * @param nodeName the node name to check
	 * @return <code>true</code> if belongs and <code>false</code> if not
	 */
	public static function isIconNode(nodeName:String):Boolean {
		return ParserFactory.getInstance().isTypeOf(nodeName, ParserFactory.ICON);
	} 

	/**
	 * Checks if passed <code>nodeName</code> belongs to icon wrapper.
	 * 
	 * @param nodeName the node name to check
	 * @return <code>true</code> if belongs and <code>false</code> if not
	 */
	public static function isIconWrapperNode(nodeName:String):Boolean {
		return ParserFactory.getInstance().isTypeOf(nodeName, ParserFactory.ICON_WRAPPER);
	} 

	/**
	 * Checks if passed <code>nodeName</code> belongs to reflection property.
	 * 
	 * @param nodeName the node name to check
	 * @return <code>true</code> if belongs and <code>false</code> if not
	 */
	public static function isPropertyNode(nodeName:String):Boolean {
		return ParserFactory.getInstance().isTypeOf(nodeName, ParserFactory.PROPERTY);
	} 

	/**
	 * Checks if passed <code>nodeName</code> belongs to reflection argument.
	 * 
	 * @param nodeName the node name to check
	 * @return <code>true</code> if belongs and <code>false</code> if not
	 */
	public static function isArgumentNode(nodeName:String):Boolean {
		return ParserFactory.getInstance().isTypeOf(nodeName, ParserFactory.ARGUMENT);
	} 
	 
}